package com.ctshk.rpc.iotc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.iotc.dto.OperatePromotionListDTO;
import com.ctshk.rpc.iotc.dto.OperatePromotionQueryDTO;
import com.ctshk.rpc.iotc.entity.OperatePromotion;
import com.ctshk.rpc.iotc.entity.OperatePromotionProduct;
import com.ctshk.rpc.iotc.mapper.OperatePromotionMapper;
import com.ctshk.rpc.iotc.mapper.OperatePromotionProductMapper;
import com.ctshk.rpc.iotc.req.IdsTwo;
import com.ctshk.rpc.iotc.req.OperatePromotionConformReq;
import com.ctshk.rpc.iotc.service.IOperatePromotionProductService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 推广码适用产品 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-22
 */
@DubboService
public class OperatePromotionProductServiceImpl extends ServiceImpl<OperatePromotionProductMapper, OperatePromotionProduct> implements IOperatePromotionProductService {

    @Autowired
    private OperatePromotionMapper operatePromotionMapper;
    @Autowired
    private OperatePromotionServiceImpl operatePromotionService;

    @Override
    public Result getOperatePromotion(Long productId) {
        QueryWrapper<OperatePromotionProduct> qw = new QueryWrapper<>();
        qw.lambda().eq(OperatePromotionProduct::getIsDeleted, IsDeletedCode.NO.getCode());
        qw.lambda().eq(OperatePromotionProduct::getRelationId, productId);
        OperatePromotionProduct operatePromotionProduct = baseMapper.selectOne(qw);
        if (operatePromotionProduct == null) {
            return Result.failed(SystemError.IOTC_40009);
        }
        return Result.success(operatePromotionProduct);
    }

    @Override
    public PageResponse<OperatePromotionListDTO> queryListAll(IdsTwo req) {
        Page<OperatePromotionProduct> page = new Page(req.getPageNo(), req.getPageSize());
        QueryWrapper<OperatePromotionProduct> qw = new QueryWrapper<>();
        qw.lambda().select(OperatePromotionProduct::getCodeId)
                .eq(OperatePromotionProduct::getIsDeleted, IsDeletedCode.NO.getCode())
                .eq(OperatePromotionProduct::getRelationId, req.getRelationId());
        List<OperatePromotionProduct> obpp = baseMapper.selectList(qw);
        if (obpp != null && obpp.size() > 0) {
            List<Long> collect = obpp.stream().map(e -> e.getCodeId()).collect(Collectors.toList());
            if (collect != null && collect.size() > 0) {
                List<OperatePromotion> result = operatePromotionMapper.selectBatchIds(collect);
                List<OperatePromotionListDTO> resultPage = EntityUtil.copyList(result, OperatePromotionListDTO.class);
                OperatePromotionConformReq opcr = EntityUtil.copy(req, OperatePromotionConformReq.class);
                resultPage.forEach(e -> {
                    opcr.setActivityCode(e.getActivityCode());
                    Result<OperatePromotionQueryDTO> res = operatePromotionService.queryConform(opcr);
                    if (res.getData() != null) {
                        e.setMoney(res.getData().getReductionAmount());
                    }
                });
                resultPage = resultPage.stream().filter(e -> e.getMoney() != null).collect(Collectors.toList());
                return new PageResponse<>(resultPage,
                        page.getCurrent() == 1, page.getTotal() <= (page.getCurrent() *
                        page.getSize()), page.getTotal(), page.getCurrent(), page.getSize());
            }
        }
        return new PageResponse<>(null,
                page.getCurrent() == 1, page.getTotal() <= (page.getCurrent() *
                page.getSize()), page.getTotal(), page.getCurrent(), page.getSize());
    }
}
